
from lib.api import *
import re
from lib.urls import *


def suite_setup():
    INFO('返回token')
    global auth_token
    auth_token = am.getToken()

class API_2001:
    name = 'API_2001'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：查询设备信息 测试     #2.1')
        INFO('前置条件：token正确，locationId不填')
        # urlpara = {         #参数为可选项
        #     'locationId':349
        # }
        response = requests.post(url_Target_Address+url_Point_getOpcDeviceList,
                                 headers={
                                     "auth-token": auth_token
                                 }
                                 )
        expectedResult = json.dumps(json.loads(am.expectedResult()[2][3]), ensure_ascii=False, indent=4)
        outExpectedResult = '预期结果:\n' + expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))          #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))


class API_2002:
    name = 'API_2002'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：查询设备信息 测试     #2.1')
        INFO('前置条件：token为空，locationId不填')
        # urlpara = {         #参数为可选项
        #     'locationId':349
        # }
        response = requests.post(url_Target_Address+url_Point_getOpcDeviceList,
                                 # headers={
                                 #     "auth-token": auth_token
                                 # }
                                 )
        expectedResult = json.dumps(json.loads(am.expectedResult()[3][3]), ensure_ascii=False, indent=4)
        outExpectedResult = '预期结果:\n' + expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))


class API_2003:
    name = 'API_2003'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：查询设备信息 测试     #2.1')
        INFO('前置条件：token过期，locationId不填')
        # urlpara = {         #参数为可选项
        #     'locationId':349
        # }
        response = requests.post(url_Target_Address+url_Point_getOpcDeviceList,
                                 headers={
                                     "auth-token": 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlS2V5IjoiIiwidXNlck5hbWUiOiJ5enlhZG1pbiIsImV4cCI6MTYyMjAwNjg2NCwidXNlcklkIjoieXp5YWRtaW4ifQ.-fJYSIJdfbiSabU71x9nbb2n2qbl8IM_s6xk0LuNVfk'
                                 }
                                 )
        expectedResult = json.dumps(json.loads(am.expectedResult()[3][3]), ensure_ascii=False, indent=4)
        outExpectedResult = '预期结果:\n' + expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))



class API_2004:
    name = 'API_2004'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：查询设备信息 测试     #2.1')
        INFO('前置条件：token正确，locationId：349 正确')
        urlpara = {         #参数为可选项
            'locationId':349
        }
        response = requests.post(url_Target_Address+url_Point_getOpcDeviceList,
                                 headers={
                                     "auth-token": auth_token
                                 },
                                 params=urlpara
                                 )
        expectedResult = json.dumps(json.loads(am.expectedResult()[2][3]), ensure_ascii=False, indent=4)
        outExpectedResult = '预期结果:\n' + expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))


class API_2005:
    name = 'API_2005'       # 测试用例名字
    def teststeps(self):

        INFO('数据点监控接口：查询设备信息 测试     #2.1')
        INFO("前置条件：token 正确，'locationId':347错误，虽然347存在，但不在目标location内")
        urlpara = {         #参数为可选项
            'locationId':347
        }
        response = requests.post(url_Target_Address+url_Point_getOpcDeviceList,
                                 headers={
                                     "auth-token": auth_token
                                 },
                                 params=urlpara
                                 )
        expectedResult = json.dumps(json.loads(am.expectedResult()[3][3]), ensure_ascii=False, indent=4)
        outExpectedResult = '预期结果:\n' + expectedResult
        INFO(outExpectedResult)
        am.printResponse(response)
        result = sorted(set(re.sub('\\s|{|}|\\[|]','',expectedResult).split(',')) ^ set(re.sub('\\s|{|}|\\[|]','',response.content.decode('utf8')).split(',')))           #对比两个字符串中的不同并打印
        INFO('\n对比结果：\n'+str(result))
        CHECK_POINT('预期结果和输出结果是否保持一致',expectedResult==response.content.decode('utf8'))
